package com.example.xuegengwang.xuegengwang.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.xuegengwang.xuegengwang.R;

import https.handler.ErrorPageHandler;
import view.MyPagerSlidingTabStrip;

import java.util.ArrayList;

import common.BaseFragment;
import common.BaseFragmentAdapter;
import thirdFragment.fragment.College;
import thirdFragment.fragment.HighSchoolEntranceExam;

/**
 * 资讯fragment
 */
@SuppressLint("ValidFragment")
public class ThirdFragment extends BaseFragment {
    private View view;
    private ViewPager third_fragment_view_pager;
    private FragmentManager fragmentManager;
    private MyPagerSlidingTabStrip fragment_third_tabs;
    private HighSchoolEntranceExam highSchoolEntranceExam;//高考
    private College college;//高校
    private ArrayList<Fragment> arrayList;
    private String[] tabs;
    private String tabs1, tabs2;
    private int tabs_color, tabs_text_size, tabs_color_chosed;
    private Activity activity;

    private ErrorPageHandler myHandler = new ErrorPageHandler(true){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_third, container, false);
        if (getActivity() != null) {
            activity = getActivity();
            initView();
            initData();
            initFragment();
            initTabStrip();
        }
        return view;
    }

    private void initData() {
        tabs1 = "高校";
        tabs2 = "高考";
        tabs_color_chosed = activity.getResources().getColor(R.color.white);
        tabs_color = activity.getResources().getColor(R.color.tab_color);
        tabs_text_size = activity.getResources().getDimensionPixelSize(R.dimen.text_size);
    }

    private void initView() {
        third_fragment_view_pager = (ViewPager) view.findViewById(R.id.third_fragment_view_pager);
        third_fragment_view_pager.setOffscreenPageLimit(2);
        fragment_third_tabs = (MyPagerSlidingTabStrip) view.findViewById(R.id.fragment_third_tabs);
    }

    private void initFragment() {
        fragmentManager = getChildFragmentManager();
        highSchoolEntranceExam = new HighSchoolEntranceExam();
        college = new College();
        arrayList = new ArrayList<>();
        arrayList.add(college);
        arrayList.add(highSchoolEntranceExam);
        tabs = new String[]{tabs1, tabs2};
        third_fragment_view_pager.setAdapter(new BaseFragmentAdapter(fragmentManager, arrayList, tabs));
        fragment_third_tabs.setViewPager(third_fragment_view_pager,myHandler);
    }

    private void initTabStrip() {
        fragment_third_tabs.setTextColor(tabs_color);
        fragment_third_tabs.setTextSize(tabs_text_size);
        fragment_third_tabs.setDividerColor(Color.TRANSPARENT);
        fragment_third_tabs.setShouldExpand(true,myHandler);
        fragment_third_tabs.setDividerPadding(1);
        fragment_third_tabs.setUnderlineHeight(0);
        fragment_third_tabs.setIndicatorColor(tabs_color_chosed);
        fragment_third_tabs.setIndicatorPadding(105, 105);
        fragment_third_tabs.setIndicatorHeight(4);
        fragment_third_tabs.setSelectedTextColor(tabs_color_chosed);
    }

}
